﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Revit;

namespace HYFloorPipeModule
{
	public class ConnectFloorPipe
	{
		public ConnectFloorPipe(Document document, double errorDis, Element elem, FloorPipeConnParam fpcp)
		{
			this.doc = document;
			this.deviation = errorDis;
			this.fiElem = elem;
			this.connSetting = fpcp;
		}

		public void DoConnect(Group group, Reference trackRefer, IList<Connector> connIns, IList<Connector> connOuts)
		{
			if (trackRefer == null)
			{
				this.ConnFloorPipe(group, connIns, connOuts);
				return;
			}
			this.ConnFloorPipeByTrack(group, trackRefer, connIns, connOuts);
		}

		public void ConnFloorPipeByTrack(Group group, Reference trackRefer, IList<Connector> connIns, IList<Connector> connOuts)
		{
			FloorPipeTools floorPipeTools = new FloorPipeTools(this.doc);
			FloorPipeGroupData elementES = floorPipeTools.GetElementES(group);
			GraphicsStyle inType;
			GraphicsStyle outType;
			if (elementES.drawFloorPipeData == null)
			{
				inType = elementES.drawYxFloorPipeData.InType;
				outType = elementES.drawYxFloorPipeData.OutType;
			}
			else
			{
				inType = elementES.drawFloorPipeData.InType;
				outType = elementES.drawFloorPipeData.OutType;
			}
			ModelLine modelLine = this.doc.GetElementById(elementES.PipeInId) as ModelLine;
			XYZ xyz = null;
			if (modelLine != null)
			{
				xyz = this.GetModelLinePnt(modelLine, 0);
			}
			if (xyz == null || this.CheckLineBeConned(modelLine, xyz))
			{
				YJKMessageBox.Information("选择的地热盘管已连接！");
				return;
			}
			SearchCurve searchCurve = new SearchCurve(modelLine, xyz, 0.032808398950131233);
			searchCurve.DoSearch();
			CurveElementNode listRoot = searchCurve.GetListRoot();
			CurveElementNode curveElementNode = null;
			this.GetBottom(listRoot, ref curveElementNode);
			ElementSet elementSet = new ElementSet();
			ICollection<ElementId> icIds = group.UngroupMembers();
			this.doc.Regenerate();
			this.AddElementInSet(icIds, ref elementSet);
			SearchCurve searchCurve2 = new SearchCurve(this.doc.GetElement(trackRefer) as CurveElement, trackRefer.GlobalPoint, 0.032808398950131233);
			searchCurve2.DoSearch();
			CurveElementNode listRoot2 = searchCurve2.GetListRoot();
			CurveElementNode curveElementNode2 = null;
			this.GetBottom(listRoot2, ref curveElementNode2);
			XYZ modelLinePnt = this.GetModelLinePnt(curveElementNode2.CurveElement, 0);
			Connector connector = null;
			Connector connector2 = null;
			connector = this.GetNearConnector(connIns, modelLinePnt, 3.2467532467532467);
			connector2 = this.GetNearConnector(connOuts, modelLinePnt, 4.87012987012987);
			if (connector == null)
			{
				throw new Exception("未找到分集水器未连接的进水连接口！");
			}
			if (connector2 == null)
			{
				throw new Exception("未找到分集水器未连接的进水连接口！");
			}
			ModelLine modelLine2;
			if (elementES.PipeLastId != null)
			{
				modelLine2 = (this.doc.GetElementById(elementES.PipeLastId) as ModelLine);
			}
			else
			{
				modelLine2 = (curveElementNode.CurveElement as ModelLine);
			}
			bool flag = this.BeInSide(connector, connector2, modelLine, this.GetModelLinePnt(modelLine2, 0));
			this.ExchangeConn(ref connector, ref connector2, flag);
			IList<ModelLine> list = this.DrawConnLine(listRoot2, listRoot.CurveElement as ModelLine, inType, 0);
			if (list == null || list.Count < 1)
			{
				throw new Exception("请选择合适的路由再进行连接。");
			}
			double distance = this.GetDistance(listRoot.CurveElement as ModelLine, modelLine2);
			IList<ModelLine> list2 = this.DrawConnLine(list, modelLine2, distance, outType, 0);
			ModelLine modelLine3 = list.Last<ModelLine>();
			ModelLine modelLine4 = list[list.Count - 2];
			IList<ModelLine> modelLines = new List<ModelLine>();
			if (connector != null)
			{
				modelLines = this.ConnEndLine(ref modelLine3, connector, inType);
			}
			IList<ModelLine> modelLines2 = new List<ModelLine>();
			ModelLine modelLine5 = list2.Last<ModelLine>();
			if (connector2 != null)
			{
				modelLines2 = this.ConnEndLine(ref modelLine5, connector2, outType);
			}
			this.doc.Regenerate();
			ModelLine modelLine6 = null;
			ModelLine modelLine7 = null;
			IList<ModelLine> list3 = this.UniteModelLineList(list, modelLines, ref modelLine6);
			list3.Insert(0, modelLine);
			IList<ModelLine> list4 = this.UniteModelLineList(list2, modelLines2, ref modelLine7);
			list4.Insert(0, modelLine2);
			double cornerRadius = this.GetCornerRadius(elementES);
			IList<Element> listLine = floorPipeTools.ChamferModelLines(list3, cornerRadius);
			IList<Element> listLine2 = floorPipeTools.ChamferModelLines(list4, cornerRadius);
			this.AddElementInSet(listLine, ref elementSet);
			this.AddElementInSet(listLine2, ref elementSet);
			if (modelLine6 != null)
			{
				elementSet.Insert(modelLine6);
			}
			if (modelLine7 != null)
			{
				elementSet.Insert(modelLine7);
			}
			elementSet = this.RemoveSpecElements(elementSet, modelLine, modelLine2);
			double modelLinesTotalLength = this.GetModelLinesTotalLength(elementSet);
			elementES.PipesLength = modelLinesTotalLength;
			list3.Remove(modelLine6);
			this.DeleteModelLines(list3);
			list4.Remove(modelLine7);
			this.DeleteModelLines(list4);
			this.doc.Regenerate();
			if (!flag)
			{
				foreach (object obj in elementSet)
				{
					ModelCurve modelCurve = obj as ModelCurve;
					if (modelCurve.LineStyle.Name.Equals(inType.Name))
					{
						modelCurve.LineStyle = outType;
					}
					else if (modelCurve.LineStyle.Name.Equals(outType.Name))
					{
						modelCurve.LineStyle = inType;
					}
				}
				this.doc.Regenerate();
			}
			Group group2 = this.doc.CreatYJKGroup(elementSet);
			floorPipeTools.SetElementES(group2, elementES);
			this.RecordConnedConnector(connector, connector2);
			this.doc.Regenerate();
		}

		private XYZ GetDefaultConnPoint(Connector conn, double pntZValue)
		{
			XYZ xyz = (this.fiElem as FamilyInstance).FacingOrientation * -1.0;
			XYZ xyz2 = conn.Origin;
			xyz2 = new XYZ(xyz2.X, xyz2.Y, pntZValue);
			double num = 1.1482939632545932;
			return xyz2.Add(num * xyz);
		}

		private bool BeInSide(Connector connIn, Connector connOut, ModelLine gpInMLine, XYZ outPoint)
		{
			XYZ xyz = YJKRevitTools.SetZToZero(this.GetModelLinePnt(gpInMLine, 0));
			XYZ xyz2 = YJKRevitTools.SetZToZero(this.GetModelLinePnt(gpInMLine, 1));
			YJKLine hyline = new YJKLine(xyz, xyz2);
			hyline.MakeUnBound();
			XYZ xyz3 = xyz.Subtract(xyz2).Normalize();
			XYZ xyz4 = YJKRevitTools.SetZToZero(connIn.Origin);
			outPoint = YJKRevitTools.SetZToZero(outPoint);
			XYZ xyz5 = hyline.GetProject(outPoint).Subtract(outPoint).Normalize();
			XYZ xyz6 = xyz3.CrossProduct(xyz5).Normalize();
			XYZ xyz7 = YJKRevitTools.SetZToZero(connOut.Origin);
			YJKLine hyline2 = new YJKLine(xyz4, xyz4.Add(this.vectorFi));
			hyline2.MakeUnBound();
			XYZ xyz8 = hyline2.GetProject(xyz7).Subtract(xyz7).Normalize();
			return !this.vectorFi.CrossProduct(xyz8).Normalize().IsAlmostEqualTo(xyz6, 0.0001);
		}

		private bool BeCheckIntersect(YJKLine hyInLine1, YJKLine hyInLine2, YJKLine hyOutLine1, YJKLine hyOutLine2)
		{
			bool result = false;
			if (hyInLine1 == null && hyInLine2 != null)
			{
				if (hyOutLine1 != null && hyOutLine2 != null)
				{
					if (hyInLine2.IntersectionPoint(hyOutLine1.Line) != null || hyInLine2.IntersectionPoint(hyOutLine2.Line) != null)
					{
						result = true;
					}
				}
				else if (hyOutLine1 != null && hyOutLine2 == null)
				{
					if (hyInLine2.IntersectionPoint(hyOutLine1.Line) != null)
					{
						result = true;
					}
				}
				else if (hyOutLine1 == null && hyOutLine2 != null && hyInLine2.IntersectionPoint(hyOutLine2.Line) != null)
				{
					result = true;
				}
			}
			else if (hyInLine1 != null && hyInLine2 == null)
			{
				if (hyOutLine1 != null && hyOutLine2 != null)
				{
					if (hyInLine1.IntersectionPoint(hyOutLine1.Line) != null || hyInLine1.IntersectionPoint(hyOutLine2.Line) != null)
					{
						result = true;
					}
				}
				else if (hyOutLine1 != null && hyOutLine2 == null)
				{
					if (hyInLine1.IntersectionPoint(hyOutLine1.Line) != null)
					{
						result = true;
					}
				}
				else if (hyOutLine1 == null && hyOutLine2 != null && hyInLine1.IntersectionPoint(hyOutLine2.Line) != null)
				{
					result = true;
				}
			}
			else if (hyOutLine1 != null && hyOutLine2 != null)
			{
				if (hyInLine1.IntersectionPoint(hyOutLine1.Line) != null || hyInLine1.IntersectionPoint(hyOutLine2.Line) != null || hyInLine2.IntersectionPoint(hyOutLine1.Line) != null || hyInLine2.IntersectionPoint(hyOutLine2.Line) != null)
				{
					result = true;
				}
			}
			else if (hyOutLine1 != null && hyOutLine2 == null)
			{
				if (hyInLine1.IntersectionPoint(hyOutLine1.Line) != null || hyInLine2.IntersectionPoint(hyOutLine1.Line) != null)
				{
					result = true;
				}
			}
			else if (hyOutLine1 == null && hyOutLine2 != null && (hyInLine1.IntersectionPoint(hyOutLine2.Line) != null || hyInLine2.IntersectionPoint(hyOutLine2.Line) != null))
			{
				result = true;
			}
			return result;
		}

		private void ExchangeConn(ref Connector connIn, ref Connector connOut, bool beSameSide)
		{
			if (!beSameSide)
			{
				Connector connector = connIn;
				connIn = connOut;
				connOut = connector;
			}
		}

		public void ConnFloorPipe(Group group, IList<Connector> connIns, IList<Connector> connOuts)
		{
			if (connIns == null || connOuts == null || connOuts.Count < 1 || connIns.Count < 1)
			{
				throw new Exception("分集水器连接口不能为空！");
			}
			FloorPipeTools floorPipeTools = new FloorPipeTools(this.doc);
			FloorPipeGroupData elementES = floorPipeTools.GetElementES(group);
			GraphicsStyle inType;
			GraphicsStyle outType;
			if (elementES.drawFloorPipeData != null)
			{
				inType = elementES.drawFloorPipeData.InType;
				outType = elementES.drawFloorPipeData.OutType;
			}
			else
			{
				inType = elementES.drawYxFloorPipeData.InType;
				outType = elementES.drawYxFloorPipeData.OutType;
			}
			ModelLine modelLine = this.doc.GetElementById(elementES.PipeInId) as ModelLine;
			XYZ xyz = null;
			if (modelLine != null)
			{
				xyz = this.GetModelLinePnt(modelLine, 0);
			}
			if (xyz == null || this.CheckLineBeConned(modelLine, xyz))
			{
				YJKMessageBox.Information("选择的地热盘管已连接！");
				return;
			}
			SearchCurve searchCurve = new SearchCurve(modelLine, xyz, 0.032808398950131233);
			searchCurve.DoSearch();
			CurveElementNode listRoot = searchCurve.GetListRoot();
			CurveElementNode curveElementNode = null;
			this.GetBottom(listRoot, ref curveElementNode);
			ElementSet elementSet = new ElementSet();
			ICollection<ElementId> icIds = group.UngroupMembers();
			this.doc.Regenerate();
			this.AddElementInSet(icIds, ref elementSet);
			Connector nearConnector = this.GetNearConnector(connIns, xyz, 0.32808398950131235);
			if (nearConnector == null)
			{
				throw new Exception("未找到分集水器未连接的进水连接口！");
			}
			Connector connector = null;
			connector = this.GetNearConnector(connOuts, this.GetModelLinePnt(modelLine, 0), 0.49212598425196846);
			if (connector == null)
			{
				throw new Exception("未找到分集水器未连接的进水连接口！");
			}
			ModelLine modelLine2;
			if (elementES.PipeLastId != null)
			{
				modelLine2 = (this.doc.GetElementById(elementES.PipeLastId) as ModelLine);
			}
			else
			{
				modelLine2 = (curveElementNode.CurveElement as ModelLine);
			}
			double distance = this.GetDistance(listRoot.CurveElement as ModelLine, modelLine2);
			ModelLine modelLine3 = listRoot.CurveElement as ModelLine;
			if (this.BeInConnLine(nearConnector.Origin, modelLine3, modelLine2))
			{
				this.GetChangedModelLine(ref modelLine3, distance, 0);
			}
			else
			{
				this.GetChangedModelLine(ref modelLine3, -distance, 0);
			}
			bool flag = this.BeInSide(nearConnector, connector, modelLine, this.GetModelLinePnt(modelLine2, 0));
			this.ExchangeConn(ref nearConnector, ref connector, flag);
			this.doc.Regenerate();
			XYZ defaultConnPoint = this.GetDefaultConnPoint(nearConnector, xyz.Z);
			IList<ModelLine> list = this.DrawConnLine(defaultConnPoint, modelLine3, inType, 0);
			ModelLine modelLine4 = list.Last<ModelLine>();
			IList<ModelLine> modelLines = new List<ModelLine>();
			if (nearConnector != null)
			{
				modelLines = this.ConnEndLine(ref modelLine4, nearConnector, inType);
			}
			IList<ModelLine> list2 = this.DrawConnLine(list, modelLine2, distance, outType, 0);
			modelLine4 = list2.Last<ModelLine>();
			IList<ModelLine> modelLines2 = new List<ModelLine>();
			if (connector != null)
			{
				modelLines2 = this.ConnEndLine(ref modelLine4, connector, outType);
			}
			this.doc.Regenerate();
			ModelLine modelLine5 = null;
			ModelLine modelLine6 = null;
			IList<ModelLine> list3 = this.UniteModelLineList(list, modelLines, ref modelLine5);
			list3.Insert(0, modelLine);
			IList<ModelLine> list4 = this.UniteModelLineList(list2, modelLines2, ref modelLine6);
			list4.Insert(0, modelLine2);
			double cornerRadius = this.GetCornerRadius(elementES);
			IList<Element> listLine = floorPipeTools.ChamferModelLines(list3, cornerRadius);
			IList<Element> listLine2 = floorPipeTools.ChamferModelLines(list4, cornerRadius);
			this.AddElementInSet(listLine, ref elementSet);
			this.AddElementInSet(listLine2, ref elementSet);
			if (modelLine5 != null)
			{
				elementSet.Insert(modelLine5);
			}
			if (modelLine6 != null)
			{
				elementSet.Insert(modelLine6);
			}
			elementSet = this.RemoveSpecElements(elementSet, modelLine, modelLine2);
			double modelLinesTotalLength = this.GetModelLinesTotalLength(elementSet);
			elementES.PipesLength = modelLinesTotalLength;
			list3.Remove(modelLine5);
			this.DeleteModelLines(list3);
			list4.Remove(modelLine6);
			this.DeleteModelLines(list4);
			this.doc.Regenerate();
			if (!flag)
			{
				foreach (object obj in elementSet)
				{
					ModelCurve modelCurve = obj as ModelCurve;
					if (modelCurve.LineStyle.Name.Equals(inType.Name))
					{
						modelCurve.LineStyle = outType;
					}
					else if (modelCurve.LineStyle.Name.Equals(outType.Name))
					{
						modelCurve.LineStyle = inType;
					}
				}
				this.doc.Regenerate();
			}
			Group group2 = this.doc.CreatYJKGroup(elementSet);
			floorPipeTools.SetElementES(group2, elementES);
			this.RecordConnedConnector(nearConnector, connector);
			this.doc.Regenerate();
		}

		private ElementSet RemoveSpecElements(ElementSet elementSet, Element elem1, Element elem2)
		{
			IList<Element> list = new List<Element>();
			foreach (object obj in elementSet)
			{
				Element element = (Element)obj;
				if (!element.Id.Equals(elem1.Id) && !element.Id.Equals(elem2.Id))
				{
					list.Add(element);
				}
			}
			ElementSet elementSet2 = new ElementSet();
			foreach (Element element2 in list)
			{
				elementSet2.Insert(element2);
			}
			return elementSet2;
		}

		private bool CheckLineBeConned(ModelLine mLine, XYZ ckPoint)
		{
			double num = 0.032808398950131233;
			IList<CurveElement> subCENodes = new CurveElementNode(mLine, ckPoint, num).GetSubCENodes(ckPoint, num);
			return subCENodes != null && subCENodes.Count >= 1;
		}

		private double GetCornerRadius(FloorPipeGroupData fpGroupData)
		{
			double result = 0.0;
			if (fpGroupData.drawFloorPipeData != null)
			{
				result = fpGroupData.drawFloorPipeData.DisRadius;
			}
			if (fpGroupData.drawYxFloorPipeData != null)
			{
				result = fpGroupData.drawYxFloorPipeData.DisRadius;
			}
			return result;
		}

		private double GetModelLinesTotalLength(ElementSet elemSet)
		{
			double num = 0.0;
			foreach (object obj in elemSet)
			{
				Element elem = (Element)obj;
                num += elem.GetCurveLength(BuiltInParameter.CURVE_ELEM_LENGTH);
			}
			return num;
		}

		private List<ModelLine> UniteModelLineList(IList<ModelLine> modelLines1, IList<ModelLine> modelLines2, ref ModelLine vecticalLine)
		{
			List<ModelLine> result = new List<ModelLine>();
			this.AddModelLineToNewList(modelLines1, ref result, true);
			vecticalLine = this.AddModelLineToNewList(modelLines2, ref result, false);
			return result;
		}

		private ModelLine AddModelLineToNewList(IList<ModelLine> modelLines, ref List<ModelLine> newModelLineList, bool orderAdd = true)
		{
			if (newModelLineList == null)
			{
				newModelLineList = new List<ModelLine>();
			}
			if (modelLines == null || modelLines.Count < 1)
			{
				return null;
			}
			ModelLine result = null;
			if (orderAdd)
			{
				IEnumerator<ModelLine> enumerator = modelLines.GetEnumerator();
				{
					while (enumerator.MoveNext())
					{
						ModelLine modelLine = enumerator.Current;
						if (!this.BeVerticalLine(modelLine))
						{
							newModelLineList.Add(modelLine);
						}
						else
						{
							result = modelLine;
						}
					}
					return result;
				}
			}
			for (int i = modelLines.Count - 1; i >= 0; i--)
			{
				if (!this.BeVerticalLine(modelLines[i]))
				{
					newModelLineList.Add(modelLines[i]);
				}
				else
				{
					result = modelLines[i];
				}
			}
			return result;
		}

		private bool BeVerticalLine(ModelLine modelLine)
		{
			bool result = false;
			XYZ modelLinePnt = this.GetModelLinePnt(modelLine, 0);
			XYZ modelLinePnt2 = this.GetModelLinePnt(modelLine, 1);
			double value = modelLinePnt.Z - modelLinePnt2.Z;
			double num = 0.016404199475065617;
			if (Math.Abs(value) > num)
			{
				result = true;
			}
			return result;
		}

		private void DeleteModelLines(IList<ModelLine> listElement)
		{
			foreach (ModelLine elem in listElement)
			{
				this.doc.DeleteElement(elem);
			}
		}

		private void GetBottom(CurveElementNode en, ref CurveElementNode elemNode)
		{
			if (en == null)
			{
				return;
			}
			elemNode = en;
			this.GetBottom(en.Next, ref elemNode);
		}

		public IList<ModelLine> DrawConnLine(XYZ connPnt, ModelLine modelLine, GraphicsStyle ghStyle, int pntIndex = 0)
		{
			Document document = modelLine.Document;
			IList<ModelLine> list = new List<ModelLine>();
			int index = 0;
			int index2 = 0;
			this.GetCurvePntIndex(ref index, ref index2, pntIndex);
			Curve curve = this.GetCurve(modelLine);
			XYZ hyendPoint = curve.GetEndPoint(index);
			XYZ hyendPoint2 = curve.GetEndPoint(index2);
			XYZ xyz = new XYZ(connPnt.X, connPnt.Y, hyendPoint.Z);
			ModelLine modelLine2 = null;
			XYZ turnPoint = this.GetTurnPoint(xyz, hyendPoint, hyendPoint2);
			if (this.JudgeTwoPointsDistance(hyendPoint, turnPoint, this.deviation))
			{
				modelLine2 = this.DrawModelLine(hyendPoint, turnPoint, ghStyle);
				new CurveElementNode(modelLine2, hyendPoint);
				list.Add(modelLine2);
			}
			if (modelLine2 == null)
			{
				if (this.JudgeTwoPointsDistance(hyendPoint, xyz, this.deviation))
				{
					modelLine2 = this.DrawModelLine(hyendPoint, xyz, ghStyle);
					new CurveElementNode(modelLine2, hyendPoint);
					list.Add(modelLine2);
				}
			}
			else if (this.JudgeTwoPointsDistance(turnPoint, xyz, this.deviation))
			{
				modelLine2 = this.DrawModelLine(turnPoint, xyz, ghStyle);
				new CurveElementNode(modelLine2, hyendPoint);
				list.Add(modelLine2);
			}
			return list;
		}

		public IList<ModelLine> DrawConnLine(CurveElementNode trackLine, ModelLine modelLine, GraphicsStyle ghStyle, int pntIndex = 0)
		{
			IList<ModelLine> list = new List<ModelLine>();
			int index = 0;
			int index2 = 0;
			this.GetCurvePntIndex(ref index, ref index2, pntIndex);
			Curve curve = this.GetCurve(modelLine);
			XYZ hyendPoint = curve.GetEndPoint(index);
			XYZ hyendPoint2 = curve.GetEndPoint(index2);
			if (trackLine == null)
			{
				return list;
			}
			trackLine = this.AnalysisCurveNode(trackLine);
			YJKLine hyline = new YJKLine(trackLine.PointIn, trackLine.PointOut);
			hyline.MakeUnBound();
			XYZ xyz = hyline.GetProject(hyendPoint);
			if (hyendPoint.DistanceTo(xyz) < 0.016404199475065617)
			{
				this.CreatModelLines(trackLine, hyendPoint, ghStyle, ref list);
				return list;
			}
			XYZ xyz2 = hyendPoint.Subtract(xyz).Normalize();
			double num = hyendPoint.DistanceTo(xyz);
			XYZ vector = trackLine.PointOut.Subtract(trackLine.PointIn).Normalize().CrossProduct(xyz2).Normalize() * -1.0;
			int num2 = this.CheckPositionRelation(trackLine, hyendPoint, hyendPoint2);
			if (trackLine.Next == null)
			{
				if (num2 != 3)
				{
					return null;
				}
				xyz = xyz.Add(xyz.Subtract(hyendPoint).Normalize() * num);
				ModelLine item = this.DrawModelLine(hyendPoint, xyz, ghStyle);
				list.Add(item);
				XYZ xyz3 = trackLine.PointOut.Subtract(trackLine.PointIn).Normalize();
				YJKLine hyline2 = new YJKLine(xyz, xyz.Add(xyz3));
				hyline2.MakeUnBound();
				XYZ project = hyline2.GetProject(trackLine.PointOut);
				item = this.DrawModelLine(xyz, project, ghStyle);
				list.Add(item);
			}
			else
			{
				switch (num2)
				{
				case 0:
				case 3:
				{
					YJKLine hyline3 = new YJKLine(hyendPoint, hyendPoint2);
					hyline3.MakeUnBound();
					num = hyline3.GetProject(trackLine.PointIn).DistanceTo(trackLine.PointIn);
					YJKLine deviateLine = this.GetDeviateLine(trackLine, num, vector);
					deviateLine.MakeUnBound();
					XYZ xyz4 = hyline3.IntersectionPoint(deviateLine.Line);
					ModelLine item2 = this.DrawModelLine(hyendPoint, xyz4, ghStyle);
					list.Add(item2);
					this.DrawParallelTrackLinesNew(trackLine, xyz4, ghStyle, num, vector, ref list);
					break;
				}
				case 1:
				case 2:
					this.DrawParallelTrackLinesNew(trackLine, hyendPoint, ghStyle, num, vector, ref list);
					break;
				}
			}
			return list;
		}

		private void CreatModelLines(CurveElementNode trackLine, XYZ point, GraphicsStyle ghStyle, ref IList<ModelLine> elemSet)
		{
			if (trackLine == null)
			{
				return;
			}
			ModelLine item = this.DrawModelLine(point, trackLine.PointOut, ghStyle);
			elemSet.Add(item);
			if (trackLine.Next != null)
			{
				this.CreatModelLines(trackLine.Next, trackLine.PointOut, ghStyle, ref elemSet);
				return;
			}
			if (trackLine.PreView != null)
			{
				item = this.DrawModelLine(trackLine.PointIn, trackLine.PointOut, ghStyle);
				elemSet.Add(item);
			}
		}

		public IList<ModelLine> DrawConnLine(IList<ModelLine> listNode, ModelLine modelLine, double distance, GraphicsStyle ghStyle, int pntIndex = 0)
		{
			IList<ModelLine> list = new List<ModelLine>();
			Curve curve = this.GetCurve(listNode.First<ModelLine>());
			XYZ xyz = this.GetCurve(modelLine).GetEndPoint(pntIndex);
			XYZ hyendPoint = curve.GetEndPoint(0);
			XYZ hyendPoint2 = curve.GetEndPoint(1);
			YJKLine hyline = new YJKLine(hyendPoint, hyendPoint2);
			hyline.MakeUnBound();
			XYZ project = hyline.GetProject(xyz);
			if (xyz.DistanceTo(project) < 0.016404199475065617)
			{
				return list;
			}
			XYZ xyz2 = xyz.Subtract(project).Normalize();
			XYZ vector = hyendPoint2.Subtract(hyendPoint).Normalize().CrossProduct(xyz2).Normalize() * -1.0;
			IList<YJKLine> moveTrackLines = this.GetMoveTrackLines(listNode, distance, vector);
			IEnumerable<XYZ> trackPoints = this.GetTrackPoints(moveTrackLines, listNode);
			YJKLine hyline2 = moveTrackLines.First<YJKLine>();
			if (!hyline2.IsPointOnCurveLine(xyz))
			{
				hyline2.MakeUnBound();
				XYZ project2 = hyline2.GetProject(xyz);
				ModelLine item = this.DrawModelLine(xyz, project2, ghStyle);
				list.Add(item);
				xyz = project2;
			}
			foreach (XYZ xyz3 in trackPoints)
			{
				if (xyz3 != null)
				{
					ModelLine item2 = this.DrawModelLine(xyz, xyz3, ghStyle);
					list.Add(item2);
					xyz = xyz3;
				}
			}
			return list;
		}

		public IList<ModelLine> ConnEndLine(ModelLine modelLine, Connector connector, GraphicsStyle ghStyle)
		{
			IList<ModelLine> list = new List<ModelLine>();
			XYZ hyendPoint = this.GetCurve(modelLine).GetEndPoint(1);
			XYZ origin = connector.Origin;
			XYZ xyz = new XYZ(origin.X, origin.Y, hyendPoint.Z);
			ModelLine modelLine2 = this.DrawModelLine(hyendPoint, xyz, ghStyle);
			if (modelLine2 != null)
			{
				list.Add(modelLine2);
			}
			modelLine2 = this.DrawMLine(xyz, origin, ghStyle);
			if (modelLine2 != null)
			{
				list.Add(modelLine2);
			}
			return list;
		}

		public IList<ModelLine> ConnEndLine(ref ModelLine modelLine, Connector conn, GraphicsStyle ghStyle)
		{
			IList<ModelLine> list = new List<ModelLine>();
			if (modelLine == null)
			{
				return list;
			}
			Curve curve = this.GetCurve(modelLine);
			XYZ xyz = curve.GetEndPoint(1);
			XYZ hyendPoint = curve.GetEndPoint(0);
			XYZ origin = conn.Origin;
			XYZ xyz2 = new XYZ(origin.X, origin.Y, xyz.Z);
			XYZ xyz3 = xyz2.Add(this.extendDis * this.vectorFi);
			YJKLine hyline = new YJKLine(xyz, hyendPoint);
			YJKLine hyline2 = new YJKLine(xyz2, xyz3);
			ModelLine modelLine2;
			if (hyline2.IsParallel(hyline.Line))
			{
				hyline.MakeUnBound();
				XYZ project = hyline.GetProject(xyz3);
				modelLine2 = this.DrawModelLine(xyz3, xyz2, ghStyle);
				if (modelLine2 != null)
				{
					list.Add(modelLine2);
				}
				if (hyline.IsPointOnLine(project))
				{
					modelLine2 = this.DrawModelLine(project, xyz3, ghStyle);
					if (modelLine2 != null)
					{
						list.Add(modelLine2);
					}
					this.ChangeModelLineCurve(ref modelLine, hyendPoint, project);
				}
				else
				{
					modelLine2 = this.DrawModelLine(project, xyz3, ghStyle);
					if (modelLine2 != null)
					{
						list.Add(modelLine2);
					}
					XYZ xyz4 = xyz.Subtract(hyendPoint).Normalize();
					XYZ xyz5 = project.Subtract(xyz).Normalize();
					if (xyz4.IsAlmostEqualTo(xyz5, 0.001))
					{
						modelLine2 = this.DrawModelLine(project, xyz, ghStyle);
						if (modelLine2 != null)
						{
							list.Add(modelLine2);
						}
					}
					else
					{
						this.ChangeModelLineCurve(ref modelLine, hyendPoint, project);
					}
				}
			}
			else
			{
				hyline2.MakeUnBound();
				XYZ project2 = hyline2.GetProject(xyz);
				if (hyline.IsPointOnLine(project2))
				{
					this.ChangeModelLineCurve(ref modelLine, hyendPoint, project2);
					xyz = project2;
				}
				if (hyline2.IsPointOnLine(project2))
				{
					modelLine2 = this.DrawModelLine(project2, xyz2, ghStyle);
					if (modelLine2 != null)
					{
						list.Add(modelLine2);
					}
					modelLine2 = this.DrawModelLine(xyz, project2, ghStyle);
					if (modelLine2 != null)
					{
						list.Add(modelLine2);
					}
				}
				else
				{
					modelLine2 = this.DrawModelLine(xyz3, xyz2, ghStyle);
					if (modelLine2 != null)
					{
						list.Add(modelLine2);
					}
					modelLine2 = this.DrawModelLine(project2, xyz3, ghStyle);
					if (modelLine2 != null)
					{
						list.Add(modelLine2);
					}
					modelLine2 = this.DrawModelLine(xyz, project2, ghStyle);
					if (modelLine2 != null)
					{
						list.Add(modelLine2);
					}
				}
			}
			modelLine2 = this.DrawMLine(xyz2, origin, ghStyle);
			if (modelLine2 != null)
			{
				list.Add(modelLine2);
			}
			return list;
		}

		private void ChangeModelLineCurve(ref ModelLine modelLine, XYZ startPnt, XYZ endPnt)
		{
			LocationCurve locationCurve = modelLine.Location as LocationCurve;
			Line curve = this.doc.CreatYJKLine(startPnt, endPnt, true);
			locationCurve.Curve = curve;
		}

		public XYZ TrackLineEndpoint(Reference refer)
		{
			SearchCurve searchCurve = new SearchCurve(this.doc.GetElement(refer), refer.GlobalPoint, 0.032808398950131233);
			searchCurve.DoSearch();
			CurveElementNode listRoot = searchCurve.GetListRoot();
			CurveElementNode curveElementNode = null;
			this.GetBottom(listRoot, ref curveElementNode);
			return this.GetCurve(curveElementNode.CurveElement).GetEndPoint(0);
		}

		public IList<Connector> GetConnectors(IList<Connector> listConn, int connType)
		{
			IList<Connector> list = new List<Connector>();
            FlowDirectionType flowDirectionType = (FlowDirectionType)1;
			if (connType == 1)
			{
                flowDirectionType = (FlowDirectionType)2;
			}
			foreach (Connector connector in listConn)
			{
				if (connector.Direction == flowDirectionType)
				{
					list.Add(connector);
				}
			}
			return list;
		}

		public bool CheckConnected(Connector conn, double dev)
		{
			bool result = false;
			if (this.GetPointNearLines(conn.Origin, dev).Count > 0)
			{
				result = true;
			}
			return result;
		}

		public IList<Element> GetPointNearLines(XYZ point, double dev)
		{
			IList<Element> list = new List<Element>();
			XYZ xyz = new XYZ(point.X - dev / 2.0, point.Y - dev / 2.0, point.Z - dev / 2.0);
			XYZ xyz2 = new XYZ(point.X + dev / 2.0, point.Y + dev / 2.0, point.Z + dev / 2.0);
			BoundingBoxIntersectsFilter boundingBoxIntersectsFilter = new BoundingBoxIntersectsFilter(new Outline(xyz, xyz2));
			IList<Element> list2 = new FilteredElementCollector(this.doc).OfCategory(BuiltInCategory.OST_Lines).WherePasses(boundingBoxIntersectsFilter).ToElements();
			if (list2.Count > 1)
			{
				foreach (Element item in list2)
				{
					list.Add(item);
				}
			}
			return list;
		}

		public Connector GetNearConnector(IList<Connector> listConn, XYZ point, double dev)
		{
			Connector result = null;
			if (listConn != null && listConn.Count > 0)
			{
				double num = 0.0;
				for (int i = 0; i < listConn.Count; i++)
				{
					XYZ origin = listConn[i].Origin;
					if (i == 0)
					{
						num = origin.DistanceTo(point);
						result = listConn[i];
					}
					else
					{
						double num2 = origin.DistanceTo(point);
						if (num2 < num)
						{
							num = num2;
							result = listConn[i];
						}
					}
				}
			}
			return result;
		}

		private void DrawConnLine(IList<ModelLine> listNode, ModelLine modelLine, double distance, XYZ vector, GraphicsStyle ghStyle, ref IList<ModelLine> modelLines)
		{
			if ((double)listNode.Count == 1.0)
			{
				this.DrawEndLine(listNode[0], modelLine, distance, vector, ghStyle, ref modelLines);
				return;
			}
		}

		private IList<YJKLine> GetMoveTrackLines(IList<ModelLine> listNode, double distance, XYZ vector)
		{
			IList<YJKLine> list = new List<YJKLine>();
			if (listNode.Count < 1)
			{
				return list;
			}
			foreach (ModelLine curveElem in listNode)
			{
				Curve curve = this.GetCurve(curveElem);
				XYZ hyendPoint = curve.GetEndPoint(0);
				XYZ hyendPoint2 = curve.GetEndPoint(1);
				XYZ v = hyendPoint2.Subtract(hyendPoint).Normalize().CrossProduct(vector).Normalize() * distance;
				PointPair pointPair = new PointPair(hyendPoint, hyendPoint2, true, false);
				pointPair.Offset(v);
				YJKLine item = new YJKLine(pointPair.First, pointPair.Second);
				list.Add(item);
			}
			return list;
		}

		private IList<XYZ> GetTrackPoints(IList<YJKLine> listLine, IList<ModelLine> listNode)
		{
			IList<XYZ> list = new List<XYZ>();
			if (listLine == null || listLine.Count < 1)
			{
				return list;
			}
			if (listLine.Count == 1)
			{
				Curve curve = this.GetCurve(listNode[0]);
				YJKLine hyline = new YJKLine(listLine[0].StartPoint, listLine[0].EndPoint);
				hyline.MakeUnBound();
				XYZ item = hyline.GetProject(curve.GetEndPoint(1));
				list.Add(item);
			}
			else
			{
				double num = (double)listLine.Count;
				int num2 = 0;
				XYZ item;
				while ((double)(num2 + 1) < num)
				{
					YJKLine hyline2 = new YJKLine(listLine[num2].StartPoint, listLine[num2].EndPoint);
					YJKLine hyline3 = new YJKLine(listLine[num2 + 1].StartPoint, listLine[num2 + 1].EndPoint);
					item = hyline2.IntersectionPoint(hyline3.Line, true);
					list.Add(item);
					num2++;
				}
				Curve curve2 = this.GetCurve(listNode.Last<ModelLine>());
				YJKLine hyline4 = new YJKLine(listLine.Last<YJKLine>().StartPoint, listLine.Last<YJKLine>().EndPoint);
				hyline4.MakeUnBound();
				item = hyline4.GetProject(curve2.GetEndPoint(1));
				list.Add(item);
			}
			return list;
		}

		private void DrawEndLine(ModelLine aloneLine, ModelLine modelLine, double distance, XYZ vector, GraphicsStyle ghStyle, ref IList<ModelLine> modelLines)
		{
			Curve curve = this.GetCurve(modelLine);
			XYZ hyendPoint;
			if (modelLines.Count < 1)
			{
				hyendPoint = curve.GetEndPoint(0);
			}
			else
			{
				hyendPoint = curve.GetEndPoint(1);
			}
			Curve curve2 = this.GetCurve(aloneLine);
			XYZ hyendPoint2 = curve2.GetEndPoint(0);
			XYZ hyendPoint3 = curve2.GetEndPoint(1);
			XYZ xyz = hyendPoint3.Subtract(hyendPoint2).Normalize();
			YJKLine hyline = new YJKLine(hyendPoint, hyendPoint + xyz);
			hyline.MakeUnBound();
			XYZ project = hyline.GetProject(hyendPoint3);
			ModelLine item = this.DrawModelLine(hyendPoint, project, ghStyle);
			modelLines.Add(item);
		}

		private IList<ModelLine> DrawParallelTrackLines(CurveElementNode rootNode, XYZ slPnt, XYZ elPnt, GraphicsStyle ghStyle)
		{
			IList<ModelLine> list = new List<ModelLine>();
			YJKLine hyline = new YJKLine(slPnt, elPnt);
			hyline.MakeUnBound();
			XYZ xyz = hyline.GetProject(rootNode.PointOut);
			double num = xyz.DistanceTo(rootNode.PointOut);
			bool flag = hyline.IsPointOnLineExtension(xyz);
			XYZ xyz2 = xyz.Subtract(slPnt).Normalize();
			XYZ xyz3 = xyz.Subtract(elPnt).Normalize();
			xyz = xyz.Add(num * xyz2);
			if (flag)
			{
				if (xyz2.IsAlmostEqualTo(xyz3, 0.0001))
				{
					ModelLine item = this.DrawModelLine(slPnt, xyz, ghStyle);
					list.Add(item);
					if (rootNode.Next != null)
					{
						this.CreatModelLineNew(rootNode.Next, xyz, ref list, ghStyle, num);
					}
				}
			}
			else
			{
				if (rootNode.Next == null)
				{
					return list;
				}
				int num2 = this.CheckPositionRelation(rootNode.Next, slPnt, elPnt);
				if (num2 == 0 || num2 == 3)
				{
					list = this.DranVerticalTrackLines(rootNode.Next, slPnt, elPnt, ghStyle);
				}
			}
			return list;
		}

		private void DrawParallelTrackLinesNew(CurveElementNode rootNode, XYZ slPnt, GraphicsStyle ghStyle, double deviate, XYZ vector, ref IList<ModelLine> elemSet)
		{
			if (rootNode == null)
			{
				return;
			}
			if (rootNode.Next == null)
			{
				XYZ xyz = rootNode.PointOut.Subtract(rootNode.PointIn).Normalize();
				YJKLine hyline = new YJKLine(slPnt, slPnt.Add(xyz));
				hyline.MakeUnBound();
				XYZ project = hyline.GetProject(rootNode.PointOut);
				ModelLine item = this.DrawModelLine(slPnt, project, ghStyle);
				elemSet.Add(item);
				return;
			}
			YJKLine deviateLine = this.GetDeviateLine(rootNode, deviate, vector);
			YJKLine deviateLine2 = this.GetDeviateLine(rootNode.Next, deviate, vector);
			deviateLine.MakeUnBound();
			deviateLine2.MakeUnBound();
			XYZ xyz2 = deviateLine.IntersectionPoint(deviateLine2.Line);
			ModelLine item2 = this.DrawModelLine(slPnt, xyz2, ghStyle);
			elemSet.Add(item2);
			slPnt = xyz2;
			this.DrawParallelTrackLinesNew(rootNode.Next, slPnt, ghStyle, deviate, vector, ref elemSet);
		}

		private YJKLine GetDeviateLine(CurveElementNode curveNode, double deviate, XYZ vector)
		{
			XYZ pointIn = curveNode.PointIn;
			XYZ pointOut = curveNode.PointOut;
			XYZ v = pointOut.Subtract(pointIn).Normalize().CrossProduct(vector).Normalize() * deviate;
			PointPair pointPair = new PointPair(pointIn, pointOut, true, false);
			pointPair.Offset(v);
			return new YJKLine(pointPair.First, pointPair.Second);
		}

		private IList<ModelLine> DranVerticalTrackLines(CurveElementNode rootNode, XYZ slPnt, XYZ elPnt, GraphicsStyle ghStyle)
		{
			IList<ModelLine> list = new List<ModelLine>();
			YJKLine hyline = new YJKLine(slPnt, elPnt);
			hyline.MakeUnBound();
			XYZ xyz = hyline.GetProject(rootNode.PointIn);
			double num = xyz.DistanceTo(rootNode.PointIn);
			XYZ xyz2 = xyz.Subtract(slPnt).Normalize();
			xyz = xyz.Add(num * xyz2);
			ModelLine modelLine = this.DrawModelLine(slPnt, xyz, ghStyle);
			if (modelLine != null)
			{
				list.Add(modelLine);
			}
			if (rootNode != null)
			{
				this.CreatModelLineNew(rootNode, xyz, ref list, ghStyle, num);
			}
			return list;
		}

		private void CreatModelLine(CurveElementNode curveNode, XYZ point, ref IList<ModelLine> elemSet, GraphicsStyle ghStyle)
		{
			if (curveNode == null)
			{
				return;
			}
			ModelLine item = this.DrawModelLine(point, curveNode.PointOut, ghStyle);
			elemSet.Add(item);
			this.CreatModelLine(curveNode.Next, curveNode.PointOut, ref elemSet, ghStyle);
		}

		private void CreatModelLineNew(CurveElementNode curveNode, XYZ point, ref IList<ModelLine> elemSet, GraphicsStyle ghStyle, double deviate)
		{
			if (curveNode == null)
			{
				return;
			}
			XYZ xyz = curveNode.PointOut.Subtract(curveNode.PointIn).Normalize();
			YJKLine hyline = new YJKLine(point, point.Add(xyz));
			hyline.MakeUnBound();
			XYZ xyz2 = hyline.GetProject(curveNode.PointOut).Add(deviate * xyz);
			ModelLine item = this.DrawModelLine(point, xyz2, ghStyle);
			elemSet.Add(item);
			this.CreatModelLineNew(curveNode.Next, xyz2, ref elemSet, ghStyle, deviate);
		}

		private int CheckPositionRelation(CurveElementNode trackNode, XYZ sPnt, XYZ ePnt)
		{
			int result = 0;
			YJKLine hyline = new YJKLine(trackNode.PointIn, trackNode.PointOut);
			YJKLine hyline2 = new YJKLine(sPnt, ePnt);
			if (hyline.IsParallel(hyline2.Line))
			{
				XYZ xyz = trackNode.PointIn.Subtract(trackNode.PointOut).Normalize();
				if (!sPnt.Subtract(ePnt).IsAlmostEqualTo(xyz, 0.001))
				{
					result = 1;
				}
				else
				{
					result = 2;
				}
			}
			else if (hyline.IsVertical(hyline2.Line))
			{
				result = 3;
			}
			return result;
		}

		private XYZ GetChangedModelLine(ref ModelLine modelLine, double indentDis, int pntIndex = 0)
		{
			XYZ result = null;
			Line changedLine = this.GetChangedLine(modelLine, indentDis, ref result, pntIndex);
			this.ChangeModelLineCurve(modelLine, changedLine);
			return result;
		}

		public void ChangeModelLineCurve(ModelLine modelLine, ref ModelLine changeModeLine)
		{
			XYZ hyendPoint = this.GetCurve(modelLine).GetEndPoint(0);
			Curve curve = this.GetCurve(changeModeLine);
			XYZ hyendPoint2 = curve.GetEndPoint(0);
			XYZ hyendPoint3 = curve.GetEndPoint(1);
			if (new YJKLine(hyendPoint2, hyendPoint3).IsPointOnLine(hyendPoint))
			{
				changeModeLine.GeometryCurve = new YJKLine(hyendPoint, hyendPoint3).Line;
			}
		}

		private Line GetChangedLine(ModelLine modelLine, double indentDis, ref XYZ snPnt, int pntIndex = 0)
		{
			int index = 0;
			int index2 = 0;
			this.GetCurvePntIndex(ref index, ref index2, pntIndex);
			Curve curve = this.GetCurve(modelLine);
			XYZ hyendPoint = curve.GetEndPoint(index);
			XYZ hyendPoint2 = curve.GetEndPoint(index2);
			if (hyendPoint.DistanceTo(hyendPoint2) - indentDis < 0.016404199475065617)
			{
				throw new Exception("缩进长度大于本身线段长度！");
			}
			XYZ xyz = hyendPoint.Subtract(hyendPoint2.Subtract(hyendPoint).Normalize() * indentDis);
			snPnt = xyz;
			return this.doc.CreatYJKLine(xyz, hyendPoint2, true);
		}

		private void ChangeModelLineCurve(ModelLine modelLine, Line line)
		{
			SubTransaction subTransaction = new SubTransaction(this.doc);
			try
			{
				subTransaction.Start();
				modelLine.GeometryCurve = line;
				subTransaction.Commit();
			}
			catch (Exception)
			{
                if (subTransaction != null && (int)subTransaction.GetStatus() == 1 && (int)subTransaction.GetStatus() != 3)
				{
					subTransaction.RollBack();
				}
				throw;
			}
		}

		private bool JudgeTwoPointsDistance(XYZ sPnt, XYZ ePnt, double deviation)
		{
			bool result = false;
			double num = sPnt.DistanceTo(ePnt);
			double num2 = deviation / 304.8;
			if (num > num2)
			{
				result = true;
			}
			return result;
		}

		private ModelLine DrawModelLine(XYZ sPnt, XYZ ePnt, GraphicsStyle ghStyle)
		{
			ModelLine modelLine = null;
			if (this.JudgeTwoPointsDistance(sPnt, ePnt, 10.0))
			{
				Line curve = this.doc.CreatYJKLine(sPnt, ePnt, true);
				modelLine = (this.doc.CreatHYModelCurve(curve) as ModelLine);
				modelLine.LineStyle = ghStyle;
			}
			return modelLine;
		}

		private bool BeInConnLine(XYZ connOrigin, ModelLine modelLine, ModelLine outLine)
		{
			XYZ modelLinePnt = this.GetModelLinePnt(modelLine, 0);
			XYZ modelLinePnt2 = this.GetModelLinePnt(outLine, 0);
			YJKLine hyline = new YJKLine(modelLinePnt, connOrigin);
			hyline.MakeUnBound();
			XYZ project = hyline.GetProject(modelLinePnt2);
			return hyline.IsPointOnLine(project);
		}

		private IList<XYZ> GetCurvePoints(Curve curve)
		{
			return new List<XYZ>
			{
				curve.GetEndPoint(0),
				curve.GetEndPoint(1)
			};
		}

		private bool ComparePoints(XYZ pnt1, XYZ pnt2)
		{
			bool result = false;
			if (Math.Abs(pnt1.Z - pnt2.Z) < 0.032808398950131233)
			{
				result = true;
			}
			return result;
		}

		private ModelLine DrawMLine(XYZ sPnt, XYZ ePnt, GraphicsStyle ghStyle)
		{
			ModelLine modelLine = null;
			if (this.JudgeTwoPointsDistance(sPnt, ePnt, 20.0))
			{
				modelLine = this.doc.CreatHYModelLine(sPnt, ePnt);
				modelLine.LineStyle = ghStyle;
			}
			return modelLine;
		}

		private CurveElementNode AnalysisCurveNode(CurveElementNode rootNode)
		{
			if (rootNode == null)
			{
				return rootNode;
			}
			while (rootNode != null && rootNode.Next != null)
			{
				XYZ xyz = rootNode.PointOut.Subtract(rootNode.PointIn).Normalize();
				CurveElementNode next = rootNode.Next;
				if (!next.PointOut.Subtract(next.PointIn).Normalize().IsAlmostEqualTo(xyz, 0.0001))
				{
					break;
				}
				rootNode.PointOut = new XYZ(next.PointOut.X, next.PointOut.Y, next.PointOut.Z);
				if (next.Next == null)
				{
					rootNode.Next = null;
					return rootNode;
				}
				rootNode.Next = next.Next;
				next.Next.PreView = rootNode;
			}
			if (rootNode.Next != null)
			{
				CurveElementNode next2 = rootNode.Next;
				this.AnalysisCurveNode(next2);
			}
			return rootNode;
		}

		private void TestNode(CurveElementNode rootNode, ref string message)
		{
			if (rootNode == null)
			{
				return;
			}
			message = string.Concat(new object[]
			{
				message,
				rootNode.PointIn,
				"\n",
				rootNode.PointOut,
				"\n"
			});
			YJKMessageBox.Show(message);
			if (rootNode.Next != null)
			{
				this.TestNode(rootNode.Next, ref message);
			}
		}

		public void GetEquipmentConn(Element elem, ref IList<Connector> listInConn, ref IList<Connector> listOutConn)
		{
			List<XYZ> connedConnector = this.GetConnedConnector();
			IEnumerable<Connector> elementAllConnector = YJKRevitTools.GetElementAllConnector(elem);
			listInConn = new List<Connector>();
			listOutConn = new List<Connector>();
			XYZ xyz = new XYZ(0.0, 0.0, -1.0);
			IList<Connector> list = new List<Connector>();
			foreach (Connector connector in elementAllConnector)
			{
				if (connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(xyz, 0.0001))
				{
					list.Add(connector);
				}
			}
			IList<Connector> list2 = new List<Connector>();
			foreach (Connector connector2 in list)
			{
				if (!this.BeConned(connector2, connedConnector))
				{
					list2.Add(connector2);
				}
			}
			listInConn = this.GetConnectors(list2, 0);
			listOutConn = this.GetConnectors(list2, 1);
			FamilyInstance familyInstance = elem as FamilyInstance;
			this.vectorFi = familyInstance.FacingOrientation * -1.0;
			this.SetNewConnPnt(list, connedConnector);
		}

		private void SetNewConnPnt(IList<Connector> interConns, List<XYZ> listPnt)
		{
			if (interConns == null || interConns.Count < 1 || listPnt == null || listPnt.Count < 1)
			{
				return;
			}
			string text = "";
			foreach (XYZ xyz in listPnt)
			{
				foreach (Connector connector in interConns)
				{
					if (xyz.IsAlmostEqualTo(connector.Origin, 0.0001))
					{
						if (string.IsNullOrEmpty(text))
						{
							text = xyz.ToString();
						}
						else
						{
							text = text + "+" + xyz.ToString();
						}
					}
				}
			}
			this.fiElem.GetParameter("已连接接口").Set(text);
		}

		private bool BeConned(Connector conn, List<XYZ> listPnt)
		{
			bool result = false;
			if (listPnt == null || listPnt.Count < 1)
			{
				return result;
			}
			foreach (XYZ xyz in listPnt)
			{
				if (conn.Origin.IsAlmostEqualTo(xyz, 0.0001))
				{
					result = true;
					break;
				}
			}
			return result;
		}

		private Curve GetCurve(CurveElement curveElem)
		{
			return (curveElem.Location as LocationCurve).Curve;
		}

		private XYZ GetModelLinePnt(CurveElement modelLine, int index)
		{
			return this.GetCurve(modelLine).GetEndPoint(index);
		}

		private void GetCurvePntIndex(ref int spIndex, ref int epIndex, int initIndex)
		{
			if (initIndex == 0)
			{
				spIndex = 0;
				epIndex = 1;
				return;
			}
			if (initIndex == 1)
			{
				spIndex = 1;
				epIndex = 0;
			}
		}

		private XYZ GetTurnPoint(XYZ connPoint, XYZ slPoint, XYZ elPoint)
		{
			XYZ xyz = slPoint.Subtract(elPoint).Normalize();
			YJKLine hyline = new YJKLine(connPoint, connPoint + xyz * 1.5);
			hyline.MakeUnBound();
			return hyline.GetProject(slPoint);
		}

		private double GetDistance(ModelLine modelLine1, ModelLine modelLine2)
		{
			Curve curve = this.GetCurve(modelLine1);
			YJKLine hyline = new YJKLine(curve.GetEndPoint(0), curve.GetEndPoint(1));
			curve = this.GetCurve(modelLine2);
			return hyline.GetDistance(curve.GetEndPoint(0));
		}

		private void AddElementInSet(ICollection<ElementId> icIds, ref ElementSet elemSet)
		{
			foreach (ElementId elementId in icIds)
			{
				elemSet.Insert(this.doc.GetElementById(elementId));
			}
		}

		private void AddElementInSet(IList<ModelLine> listLine, ref ElementSet elemSet)
		{
			foreach (ModelLine modelLine in listLine)
			{
				elemSet.Insert(modelLine);
			}
		}

		private void AddElementInSet(IList<Element> listLine, ref ElementSet elemSet)
		{
			foreach (Element element in listLine)
			{
				elemSet.Insert(element);
			}
		}

		private void GetEndNode(CurveElementNode en, ref CurveElementNode elemNode)
		{
			if (en == null)
			{
				return;
			}
			elemNode = en;
			this.GetEndNode(en.Next, ref elemNode);
		}

		private void RecordConnedConnector(Connector connIn, Connector connOut)
		{
			Parameter parameter = this.fiElem.GetParameter("已连接接口");
			if (parameter == null)
			{
				return;
			}
			string text = parameter.AsString();
			if (string.IsNullOrEmpty(text))
			{
				text = connIn.Origin.ToString() + "+" + connOut.Origin.ToString();
			}
			else
			{
				text = string.Concat(new string[]
				{
					text,
					"+",
					connIn.Origin.ToString(),
					"+",
					connOut.Origin.ToString()
				});
			}
			parameter.Set(text);
		}

		private List<XYZ> GetConnedConnector()
		{
			List<XYZ> list = new List<XYZ>();
			Parameter parameter = this.fiElem.GetParameter("已连接接口");
			if (parameter == null)
			{
				return null;
			}
			string text = parameter.AsString();
			if (string.IsNullOrEmpty(text))
			{
				return null;
			}
			foreach (string text2 in text.Split(new char[]
			{
				'+'
			}))
			{
				int length = text2.Length - 2;
				string[] array2 = text2.Substring(1, length).Split(new char[]
				{
					','
				});
				XYZ item = new XYZ(Convert.ToDouble(array2[0]), Convert.ToDouble(array2[1]), Convert.ToDouble(array2[2]));
				list.Add(item);
			}
			return list;
		}

		private Document doc;

		private double deviation = 20.0;

		private XYZ vectorFi;

		private double extendDis = 1.1482939632545932;

		private Element fiElem;

		private FloorPipeConnParam connSetting;
	}
}
